﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NLLSite.Administrator.UserControl
{
    public partial class ChangeInfo : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DAL_Class.Administrator objA = (DAL_Class.Administrator)Session["objadmin"];
            txtID.Text = objA.ID.ToString();
            txtTaiKhoan.Text = objA.Username.ToString();
           // txtMatKhau.Text = objA.Password.ToString();
            txtRole.Text = objA.Role.ToString();
            txtTen.Text = objA.FullName.ToString();
            txtDienThoai.Text = objA.Phone.ToString();
            txtEmail.Text = objA.Email.ToString();

            bool status = objA.Status;
            if (status)
            {
                CheckBox2.Checked = true;
            }
            else
            {
                CheckBox2.Checked = false;
            }
            Panel1.Visible = true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            DAL_Class.Admin objAdmin = new DAL_Class.Admin();
            DAL_Class.Administrator ad = new DAL_Class.Administrator();
            bool status = false;
            if (CheckBox2.Checked)
            {
                status = true;
            }
            else
            {
                status = false;
            }
            ad.ID = Convert.ToInt32(txtID.Text);
            ad.Username = txtTaiKhoan.Text;
            ad.Password = "";
            if (txtMatKhau.Text != "")
            {
                ad.Password = GetMD5Hash(txtMatKhau.Text);
            }
            ad.FullName = txtTen.Text;
            ad.Phone = txtDienThoai.Text;
            ad.Email = txtEmail.Text;
            ad.Status = status;
            Session["objadmin"] = ad;
           Label1.Text =  objAdmin.UpdateAdmin(ad);
           
            
        }
        public static string GetMD5Hash(string input)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
            bs = x.ComputeHash(bs);
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            string password = s.ToString();
            return password;
        }
    }
}